/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package snmpapplication.log;

import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.Calendar;
import java.util.GregorianCalendar;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 * @author APetcu
 */
public class Logging {

    private boolean isInit = false;
    private String logPath = "C://".concat("logFrom" + System.currentTimeMillis() + ".txt");

    public Logging() {
        try {
            // create new log file 
            File logginFile = new File(logPath);

            logginFile.createNewFile();
        } catch (IOException ex) {
            Logger.getLogger(Logging.class.getName()).log(Level.SEVERE, null, ex);
        }

        this.isInit = true;

    }

    public void log(Severity s, String message) {

        if (!this.isInit) {
            return;
        }
        try {
            // deschid flux la fisier
            PrintWriter logFlow = new PrintWriter(new FileWriter(new File(logPath), true));
            logFlow.println(s.toString() + getCurrentTime() + message);

            logFlow.flush();
            logFlow.close();
        } catch (IOException ex) {
            Logger.getLogger(Logging.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    private String getCurrentTime() {

        Calendar calendar = new GregorianCalendar();
        String am_pm;
        int day = calendar.get(Calendar.DAY_OF_MONTH);
        int month = calendar.get(Calendar.MONTH) + 1;
        int year = calendar.get(Calendar.YEAR);

        int hour = calendar.get(Calendar.HOUR);
        int minute = calendar.get(Calendar.MINUTE);
        int second = calendar.get(Calendar.SECOND);
        if (calendar.get(Calendar.AM_PM) == 0) {
            am_pm = "AM";
        } else {
            am_pm = "PM";
        }

        String s = day + "/" + month + "/" + year + "/";
        return "@" + s + hour + ":" + minute + ":" + second + " " + am_pm + "-msg-> ";
    }
}
